'''
You are given an array strarr of strings and an integer k. Your task is to return the first longest string consisting of k consecutive strings taken in the array.

Example:
longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2) --> "abigailtheta"

n being the length of the string array, if n = 0 or k > n or k <= 0 return "".

Note
consecutive strings : follow one after another without an interruption
'''

'''
基本思路：
1、首先判断 return "" 的情况
2、变量字符串数组，统计每个字符串的长度，并存储为一个单独的数组
3、遍历长度数组，并将连续k个长度相加，获取最长的索引组合(只获取第一个即可)
4、从字符串数组中将这个连续字符串取出拼接成一个，并返回
'''
# 方式一：
def longest_consec(strarr, k):
    # your code
    n = len(strarr)
    if n == 0 or k > n or k <= 0 :
        return ""
    
    # 生成长度list
    count_arr = []
    for index in range(n-k+1):
        tmp = index + k
        count_arr.append( len("".join(strarr[index:tmp])) )
    
    # 获取最长的索引
    max_index_start = count_arr.index(max(count_arr))
    max_index_end = max_index_start + k
    return "".join(strarr[max_index_start:max_index_end])



# 更好的方法

def longest_consec(strarr, k):
    result = ""
    
    if k > 0 and len(strarr) >= k:
        for index in range(len(strarr) - k + 1):
            s = ''.join(strarr[index:index+k])
            if len(s) > len(result):
                result = s
            
    return result


# Test
# strarr = ["ejjjjmmtthh", "zxxuueeg", "aanlljrrrxx", "dqqqaaabbb", "oocccffuucccjjjkkkjyyyeehh"]

# longest_consec(strarr,1)

# 总结：即使大体思路相同，也总是有更加优秀的实现方式